package org.vy.dal.dao;

import java.util.List;

import org.vy.dal.QueryManager;

public class VyDabbaSubscriptionDAO {
	private static final String FIND_BY_PK = "SELECT * FROM <TABLE> WHERE DABBA_SUBSCRIPTION_ID = m_DabbaSubscriptionId";
	private static final String FIND_BY_ID = "SELECT * FROM <TABLE> WHERE SUBSCRIBER_ID = m_SubscriberId AND IS_ACTIVE = TRUE";
	private static final String UPDATE = "UPDATE <TABLE> SET SUBSCRIBER_ID = m_SubscriberId, ID_TYPE = m_IdType, REF_VY_ID = m_RefVyId, DABBA_KEY = m_DabbaKey, START_DATE = m_StartDate, END_DATE = m_EndDate, ADDRESS_ID = m_AddressId, IS_ACTIVE = m_IsActive, LAST_UPDATED_ON = m_LastUpdatedOn, SANKALP_RASHI = m_SankalpRashi, LAST_UPDATED_BY = m_LastUpdatedBy where DABBA_SUBSCRIPTION_ID = m_DabbaSubscriptionId";
	private static final String INSERT = "INSERT INTO <TABLE> (DABBA_SUBSCRIPTION_ID, SUBSCRIBER_ID, ID_TYPE, REF_VY_ID, DABBA_KEY, START_DATE, END_DATE, ADDRESS_ID, IS_ACTIVE, SANKALP_RASHI, LAST_UPDATED_BY, LAST_UPDATED_ON) VALUES ( m_DabbaSubscriptionId, m_SubscriberId, m_IdType, m_RefVyId, m_DabbaKey, m_StartDate, m_EndDate, m_AddressId, true, m_SankalpRashi, m_LastUpdatedBy, m_LastUpdatedOn )";
	private static final String DELETE = "UPDATE <TABLE> SET IS_ACTIVE = FALSE WHERE DABBA_SUBSCRIPTION_ID = m_DabbaSubscriptionId";

	public static VyDabbaSubscriptionDO findByPK(String key) throws Exception {
		VyDabbaSubscriptionDO dto = new VyDabbaSubscriptionDO();
		dto.setDabbaSubscriptionId(key);
		dto = (VyDabbaSubscriptionDO) QueryManager
				.executeQuery(dto, FIND_BY_PK);
		return dto;
	}

	public static List<VyDabbaSubscriptionDO> findActiveById(String vyId)
			throws Exception {
		VyDabbaSubscriptionDO dto = new VyDabbaSubscriptionDO();
		dto.setSubscriberId(vyId);
		return (List<VyDabbaSubscriptionDO>)QueryManager.executeListQuery(dto,FIND_BY_ID);
	}

	public static VyDabbaSubscriptionDO insert(VyDabbaSubscriptionDO dto)
			throws Exception {

		dto = (VyDabbaSubscriptionDO) QueryManager.executeInsert(dto, INSERT);
		return dto;
	}

	public static VyDabbaSubscriptionDO update(VyDabbaSubscriptionDO dto)
			throws Exception {
		int i = QueryManager.executeUpdate(dto, UPDATE);
		if (i > 0)
			return dto;
		else
			return null;
	}

	public static VyDabbaSubscriptionDO delete(String key) throws Exception {
		VyDabbaSubscriptionDO dto = new VyDabbaSubscriptionDO();
		dto.setDabbaSubscriptionId(key);
		boolean i = QueryManager.executeDelete(dto, DELETE);
		if (i == true)
			return dto;
		else
			return null;
	}

}
